package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;

import java.math.BigDecimal;
import java.util.EventObject;

//差旅报销单：误餐补贴金额计算逻辑
public class TravelMealAllowancePlugin extends AbstractFormPlugin {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Label delexpense = this.getControl("delexpense");//差旅明细删除标签
        delexpense.addClickListener(this);
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String key = source.getKey();
        if ("delexpense".equals(key)) {
            //差旅明细删除标签
            DynamicObjectCollection tripEntryCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("tripentry");//行程信息分录
            for (int i = 0; i < tripEntryCollection.size(); i++) {
                DynamicObject tripEntry = tripEntryCollection.get(i);
                int parentRowIndex = i;
                DynamicObjectCollection entryEntityCollection = tripEntry.getDynamicObjectCollection("entryentity");//差旅明细分录
                for (int j = 0; j < entryEntityCollection.size(); j++) {
                    DynamicObject dataEntity = entryEntityCollection.get(j);
                    DynamicObject expenseitem = dataEntity.getDynamicObject("expenseitem");//费用名目
                    if (expenseitem == null) {
                        return;
                    }
                    String expenseItem = expenseitem.getString("number");
                    int rowIndex = j;
                    BigDecimal tripstandardamount = dataEntity.getBigDecimal("tripstandardamount");//差旅标准金额
                    if (expenseItem.equals("010")) {
                        BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals");//补贴餐数
                        BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                        this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                        this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                    } else if (expenseItem.equals("011")) {
                        BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals2");//补贴餐数
                        BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                        this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                        this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                    } else if (expenseItem.equals("012")) {
                        BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals3");//补贴餐数
                        BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                        this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                        this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                    }
                }
            }
            this.getView().updateView("entryentity"); // 差旅明细
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldKey = e.getProperty().getName();
        if ("trip2travelers".equals(fieldKey) || "qeug_meals".equals(fieldKey) || "qeug_meals2".equals(fieldKey) || "qeug_meals3".equals(fieldKey)) {
            //出差人，补贴餐数早、中、晚
            ChangeData[] changeSet = e.getChangeSet();
            ChangeData changeData = changeSet[0];
            int parentRowIndex = changeData.getParentRowIndex();//行程信息行
            int rowIndex = changeData.getRowIndex();//差旅明细行
            DynamicObject dataEntity = changeData.getDataEntity();//行数据
            DynamicObject expenseitem = dataEntity.getDynamicObject("expenseitem");//费用名目
            if (expenseitem == null) {
                return;
            }
            String expenseItem = expenseitem.getString("number");
            BigDecimal tripstandardamount = dataEntity.getBigDecimal("tripstandardamount");//差旅标准金额
            if (expenseItem.equals("010")) {
                BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals");//补贴餐数
                BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                this.getModel().setValue("caldaycount", qeug_meals, rowIndex, parentRowIndex);//标准天数
            } else if (expenseItem.equals("011")) {
                BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals2");//补贴餐数
                BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                this.getModel().setValue("caldaycount", qeug_meals, rowIndex, parentRowIndex);//标准天数
            } else if (expenseItem.equals("012")) {
                BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals3");//补贴餐数
                BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                this.getModel().setValue("caldaycount", qeug_meals, rowIndex, parentRowIndex);//标准天数
            }
        } else if ("istravelers".equals(fieldKey) || "caldaycount".equals(fieldKey)) {
            //多出差人、标准天数
            DynamicObjectCollection tripEntryCollection = this.getModel().getEntryEntity("tripentry");//行程信息
            if (tripEntryCollection != null) {
                for (int parentRowIndex = 0; parentRowIndex < tripEntryCollection.size(); parentRowIndex++) {
                    DynamicObject tripEntry = tripEntryCollection.get(parentRowIndex);
                    DynamicObjectCollection entryEntityCollection = tripEntry.getDynamicObjectCollection("entryentity");//差旅明细
                    if (entryEntityCollection != null) {
                        for (int rowIndex = 0; rowIndex < entryEntityCollection.size(); rowIndex++) {
                            DynamicObject dataEntity = entryEntityCollection.get(rowIndex);
                            DynamicObject expenseitem = dataEntity.getDynamicObject("expenseitem");//费用名目
                            if (expenseitem == null) {
                                continue;
                            }
                            String expenseItem = expenseitem.getString("number");
                            BigDecimal tripstandardamount = dataEntity.getBigDecimal("tripstandardamount");//差旅标准金额
                            if (expenseItem.equals("010")) {
                                BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals");//补贴餐数
                                BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                                this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                                this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                                this.getModel().setValue("caldaycount", qeug_meals, rowIndex, parentRowIndex);//标准天数
                            } else if (expenseItem.equals("011")) {
                                BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals2");//补贴餐数
                                BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                                this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                                this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                                this.getModel().setValue("caldaycount", qeug_meals, rowIndex, parentRowIndex);//标准天数
                            } else if (expenseItem.equals("012")) {
                                BigDecimal qeug_meals = dataEntity.getBigDecimal("qeug_meals3");//补贴餐数
                                BigDecimal multiply = qeug_meals.multiply(tripstandardamount);
                                this.getModel().setValue("orientryamount", multiply, rowIndex, parentRowIndex);//报销金额
                                this.getModel().setValue("overdesc", multiply, rowIndex, parentRowIndex);//超标说明
                                this.getModel().setValue("caldaycount", qeug_meals, rowIndex, parentRowIndex);//标准天数
                            }
                        }
                    }
                }
            }
        }
    }
}
